from src.handler.sprite import SpriteHandler
from pymunk.vec2d import Vec2d
import pymunk

class Block:

    def __init__(self, x=None, y=None, typ=None, needUpdate=None):
        self.x = x
        self.y = y
        self.type = typ
        self.needUpdate = needUpdate

    def onClick(self, world, x, y):
        pass

    def writeToPSO(self, data):
        data.type = self.type
        data.set("x", self.x)
        data.set("y", self.y)

    @staticmethod
    def loadFromPSO(data):
        block = Block()
        block.readFromPSO(data)
        return block

    def readFromPSO(self, data):
        self.type = data.type
        self.x = data.get("x")
        self.y = data.get("y")

    def update(self, world):
        pass

    def render(self, client, viewport):
        try:
            localX, localY = viewport.globalToLocale(self.x, self.y)
            sprite = SpriteHandler.get(self.type)
            client.window.surface.blit(sprite, (localX * 64, localY * 64))
        except:
            print("couldn't render block with type: " + self.type)
